Skip to main content

The Empty SC

The smallest smart contract is an empty smart contract with no functionalities. Compiled, it is a binary that is accepted by the blockchain.

/// An empty contract. To be used as a template when starting a new contract from scratch.
#[multiversx_sc::contract]
pub trait EmptyContract {
#[init]
fn init(&self) {}
}

Here is the smart contract code listed above and here are all the files needed for compilation.

We have the addnotation #[init] where we specify the constructor function. Only one constructor is allowed per smart contract.

Let's build the smart contract:

costin@Byblos:~/mvx/mx-contracts-rs/contracts/empty$ sc-meta all build
/home/costin/mvx/mx-contracts-rs/contracts/empty

Found 1 contract crates.

(1/1)
In /home/costin/mvx/mx-contracts-rs/contracts/empty/meta
Calling `cargo run build`
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `/home/costin/mvx/mx-contracts-rs/target/debug/empty-meta build`
Building empty.wasm in /home/costin/mvx/mx-contracts-rs/contracts/empty/wasm ...
RUSTFLAGS="-C link-arg=-s -C link-arg=-zstack-size=131072" cargo build --target=wasm32-unknown-unknown --release
Compiling proc-macro2 v1.0.69
Compiling unicode-ident v1.0.12
Compiling syn v1.0.109
Compiling version_check v0.9.4
Compiling hex v0.4.3
Compiling autocfg v1.1.0
Compiling smallvec v1.11.1
Compiling cfg-if v1.0.0
Compiling once_cell v1.18.0
Compiling endian-type v0.1.2
Compiling nibble_vec v0.1.0
Compiling zerocopy v0.7.25
Compiling arrayvec v0.7.4
Compiling radix_trie v0.2.1
Compiling hex-literal v0.3.4
Compiling ahash v0.8.6
Compiling bitflags v1.3.2
Compiling num-traits v0.2.17
Compiling quote v1.0.33
Compiling hashbrown v0.13.2
Compiling multiversx-sc-codec-derive v0.18.1
Compiling multiversx-sc-derive v0.44.0
Compiling multiversx-sc-codec v0.18.1
Compiling multiversx-sc v0.44.0
Compiling empty v0.0.0 (/home/costin/mvx/mx-contracts-rs/contracts/empty)
Compiling multiversx-sc-wasm-adapter v0.44.0
Compiling empty-wasm v0.0.0 (/home/costin/mvx/mx-contracts-rs/contracts/empty/wasm)
Finished release [optimized] target(s) in 5.28s
Copying ../wasm/target/wasm32-unknown-unknown/release/empty_wasm.wasm to ../output/empty.wasm ...
Calling wasm-opt on ../output/empty.wasm ...
Extracting imports to ../output/empty.imports.json ...
Checking EI version: 1.2 ... OK
Packing ../output/empty.mxsc.json ...
Contract size: 232 bytes.

The resulted contract is output/empty.wasm, a WebAseembly binary module that is only 232 bytes:

costin@Byblos:~/mvx/mx-contracts-rs/contracts/empty$ ls -l output/
total 16
-rw-rw-r-- 1 costin costin 838 dec 6 16:48 empty.abi.json
-rw-rw-r-- 1 costin costin 60 dec 6 16:48 empty.imports.json
-rw-rw-r-- 1 costin costin 1406 dec 6 16:48 empty.mxsc.json
-rwxrwxr-x 1 costin costin 232 dec 6 16:48 empty.wasm

Practice

  • Clone the Empty SC repo;
  • Compile the contract.